package model;

import mappers.EventMapper;
import mappers.MatchMapper;
import java.util.ArrayList;

public class Match {

    private int matchId;
    private Club awayTeam;
    private Club homeTeam;
    private int tournamentRound;
    private String location;
    private boolean isEnded;
    private String result;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    //Bruges når der ikke er tildelt turneringsrunder til holdene.
    public Match(int matchId, Club awayTeam, Club homeTeam, String location, boolean isEnded) {
        this.matchId = matchId;
        this.awayTeam = awayTeam;
        this.homeTeam = homeTeam;
        this.location = location;
        this.isEnded = isEnded;
    }

    //Bruges når der er tildelt turneringsrunder.
    public Match(int matchId, Club awayTeam, Club homeTeam, String location, int tournamentRound, boolean isEnded) {
        this.matchId = matchId;
        this.awayTeam = awayTeam;
        this.homeTeam = homeTeam;
        this.location = location;
        this.tournamentRound = tournamentRound;
        this.isEnded = isEnded;
    }

    public boolean isEnded() {
        return isEnded;
    }

    public void setEnded(boolean isEnded) {
        this.isEnded = isEnded;
    }

    public int getMatchId() {
        return matchId;
    }

    public void setMatchId(int matchId) {
        this.matchId = matchId;
    }

    public Club getAwayTeam() {
        return awayTeam;
    }

    public void setAwayTeam(Club awayTeam) {
        this.awayTeam = awayTeam;
    }

    public Club getHomeTeam() {
        return homeTeam;
    }

    public void setHomeTeam(Club homeTeam) {
        this.homeTeam = homeTeam;
    }

    public int getTournamentRound() {
        return tournamentRound;
    }

    public void setTournamentRound(int tournamentRound) {
        this.tournamentRound = tournamentRound;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        String str = "";
        if (isEnded == true) {
            str = homeTeam + " - " + awayTeam + ": " + result;
        } else {
            str = homeTeam + " - " + awayTeam;
        }
        return str;
    }

    public void calculateResult() {
        ArrayList<Event> events = EventMapper.getInstance().getMatchEvents(this);
        int countHome = 0;
        int countAway = 0;

        // Her hentes alle scoringer og selvmål, ud fra listen af events på en given kamp.
        for (int i = 0; i < events.size(); i++) {
            //Ved scoringer for holdet der har scoret et point.
            if (events.get(i).getPlayer().getClub() == homeTeam && events.get(i).getEventType().equals("Scoring")) {
                countHome++;
            } else if (events.get(i).getPlayer().getClub() == awayTeam && events.get(i).getEventType().equals("Scoring")) {
                countAway++;
            }

            //Ved selvmål for modstanderen til dem der har lavet selvmål et point.
            if (events.get(i).getPlayer().getClub() == homeTeam && events.get(i).getEventType().equals("Selvmål")) {
                countAway++;
            } else if (events.get(i).getPlayer().getClub() == awayTeam && events.get(i).getEventType().equals("Selvmål")) {
                countHome++;
            }
        }

        //Her tildeles de point som er specificeret i casen, altså 3 til vinderen,
        //eller 1 til hver hvis kampen blev uafgjort.
        if (countHome > countAway) {
            homeTeam.addPoints(3);
        } else if (countHome == countAway) {
            homeTeam.addPoints(1);
            awayTeam.addPoints(1);
        } else {
            awayTeam.addPoints(3);
        }

        result = countHome + " - " + countAway;

        MatchMapper.getInstance().endMatch(this);
    }

}
